﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Enterprise.Management.Interfaces;
using Enterprise.Management.Common;
using System.Globalization;

namespace Enterprise.Management.Caches
{
    public class LoginCookieCache : ILoginCache
    {
        private const double COOKIE_EXPIRED = 300;
        private const string USER_ID = "user-id";
        private const string EXPIRED_TIME = "expired-time";
        private const string LOGIN_STATUS_KEY = "login-key";
        private const string COOKIE_LOGIN = "B6A12B05-3FAD-4735-9AF6-7474A4187E32";

        public void Cache(int userid)
        {
            string content = GetHashString(userid, DateTime.Now.AddSeconds(COOKIE_EXPIRED));

            string key = Security.MD5(content);
            string value = string.Format("{0}&{1}={2}", content, LOGIN_STATUS_KEY, HttpUtility.UrlEncode(key));

            value = Security.Encrypt(value);

            CookieCache.SetCookie(COOKIE_LOGIN, value);
        }

        public int GetUserId()
        {
            try
            {
                string content = CookieCache.GetCookie(COOKIE_LOGIN).ToSafeString();
                if (string.IsNullOrEmpty(content)) return 0;

                content = Security.Decrypt(content);

                int userid = 0;
                DateTime expiredTime = new DateTime();
                string hash = string.Empty;
                foreach (var item in content.Split('&'))
                {
                    string[] tmp = item.Split('=');
                    string key = tmp[0];
                    string value = tmp[1];

                    if (key == USER_ID)
                    {
                        userid = Convert.ToInt32(value);
                        if (userid <= 0) return 0;
                    }
                    else if (key == EXPIRED_TIME)
                    {
                        expiredTime = new DateTime(Convert.ToInt64(value));
                        if (expiredTime < DateTime.Now) return 0;
                    }
                    else if (key == LOGIN_STATUS_KEY)
                    {
                        hash = HttpUtility.UrlDecode(value);
                    }
                }

                string hashString = GetHashString(userid, expiredTime);
                if (!Security.CompareMD5(hashString, hash)) return 0;

                Cache(userid);

                return userid;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return 0;
            }
        }

        public void Clear()
        {
            CookieCache.DeleteCookie(COOKIE_LOGIN);
        }

        private string GetHashString(int userid, DateTime expiredTime)
        {
            string userParam = UrlParam.CreateParam(USER_ID, userid);
            string accessParam = UrlParam.CreateParam(EXPIRED_TIME, expiredTime.Ticks);
            return userParam + "&" + accessParam;
        }
    }
}